package logica;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Random;

public class Mazo {

	private HashMap<CartaKey, Carta> mazo;
	
	//Constructor
	public Mazo() {
		this.mazo = new HashMap<CartaKey, Carta>();
		this.barajar();
	}
	
	private CartaKey armarKey(Carta c) {
		return new CartaKey(c.getPalo(), c.getValorPoker());
	}
	
	private void barajar() {
		MazoHelper mazoHelper = new MazoHelper();
		mazo = new HashMap<CartaKey, Carta>();
		
		int limit = mazoHelper.getMAZO_LENGTH();
		int palosLength = mazoHelper.getPALOS_LENGTH();
		int valuesLength = mazoHelper.getVALUES_LENGTH();
		String[] simbolosPoker = mazoHelper.getSIMBOLOS_POKER();
		String[] palos = mazoHelper.getPalos();
		Random random = new Random();
		HashMap<String, Integer> values = mazoHelper.getValoresPokerValoresJuego();
		
		for(int i = 0; i < limit; i++) {
			Carta cartaAux;
			
			do {
				int paloNum = random.nextInt(palosLength);
				int valueNum = random.nextInt(valuesLength);
				String valuePoker = simbolosPoker[valueNum];
				
				String palo = palos[paloNum];
				Integer valueJuego = values.get(valuePoker);
				
				cartaAux = new Carta(palo, valueJuego, valuePoker);
				
			} while(this.memberMazo(cartaAux));
			
			this.insertMazo(cartaAux);
		}
		
		/*CartaKey key2 = new CartaKey("Corazones", 4);
		CartaKey key3 = new CartaKey("Treboles", 2);
		CartaKey key = new CartaKey("Picas", 2);
		
		Carta carta2 = new Carta("Corazones", 4, 4);
		Carta carta3 = new Carta("Treboles", 4, 4);
		Carta carta = new Carta("Picas", 2, 2);
		
		this.mazo.put(key3, carta3);
		this.mazo.put(key2, carta2);
		this.mazo.put(key, carta);*/
		
	}
	
	//Metodos primitivos
	public boolean memberMazo(Carta c) {
		return this.mazo.containsKey(this.armarKey(c));
	}
	
	public HashMap<CartaKey, Carta> insertMazo(Carta c) {
		this.mazo.put(this.armarKey(c), c);
		return this.mazo;
	}
	public HashMap<CartaKey,Carta> modifyMazo(Carta c) {
		return this.insertMazo(c);
	}	
	public void showMazo() {
		Iterator<Carta> iter = (Iterator) this.mazo.entrySet().iterator();

		while(iter.hasNext()) {
			Map.Entry e = (Map.Entry) iter.next();
			Carta c = (Carta) e.getValue();
			System.out.println(c.getValorPoker() + " de " + c.getPalo());
		}
	}
	
}
